package cn.tedu.overhaul.management.controller;

import cn.tedu.overhaul.management.pojo.dto.ToolAddNewDTO;
import cn.tedu.overhaul.management.pojo.dto.ToolUpdateDTO;
import cn.tedu.overhaul.management.pojo.vo.ToolAllVO;
import cn.tedu.overhaul.management.pojo.vo.ToolVO;
import cn.tedu.overhaul.management.service.IOverhaulToolService;
import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/overhaul/Tool")
@Api(tags = "03. 工具记录模块")
@Validated
public class OverhaulToolController {

    @Autowired
    IOverhaulToolService iOverhaulToolService;
    @Autowired
    UploadController uploadController;

    @PostMapping("/add-new")
    @ApiOperation("添加工具")
    @PreAuthorize("hasAuthority('/overhaul/Tool/add-new')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@ModelAttribute @Valid ToolAddNewDTO toolAddNewDTO) {
        log.debug("开始处理[添加工具] 参数:{}", toolAddNewDTO);
        iOverhaulToolService.addNew(toolAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询借用记录")
    @ApiOperationSupport(order = 300)
    public JsonResult<PageData<ToolVO>> selectRent(@Range(min=1,message="请提交有效的页码值!")Integer page, String queryType){
        log.debug("开始处理【查询检修记录表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<ToolVO> pageData;
        if ("all".equals(queryType)) {
            pageData =iOverhaulToolService.selectRent(pageNum, Integer.MAX_VALUE);
            log.debug("*************************************pageData1：{}", pageData);
        }else{
            pageData =iOverhaulToolService.selectRent(pageNum);
            log.debug("*************************************pageData2：{}", pageData);

        }
        return JsonResult.ok(pageData);
    }



    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除工具记录")
    @PreAuthorize("hasAuthority('/overhaul/Tool/delete')")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id,@RequestParam("url") String url) {
        log.debug("开始处理【根据ID删除工具记录】的请求，参数：{}", id);
        iOverhaulToolService.delete(id);
        log.debug("参数url：{}", url);
        uploadController.remove(url);
        return JsonResult.ok();
    }

    @GetMapping("/All/{id:[0-9]+}")
    @ApiOperation("查询工具记录")
    @ApiOperationSupport(order = 320)
    public JsonResult<ToolAllVO> getStandardByIdAll(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id){
        log.debug("开始处理【根据ID修改面板查询工具记录】的请求，参数：{}", id);
        ToolAllVO queryResult=iOverhaulToolService.getStandardByIdAll(id);
        return JsonResult.ok(queryResult);
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改工具记录")
    @PreAuthorize("hasAuthority('/overhaul/Tool/update')")
    @ApiOperationSupport(order = 330)
    public JsonResult<Void> updateInfoById(@PathVariable Long id , @Valid ToolUpdateDTO toolUpdateDTO){
        log.debug("开始处理【修改工具记录】的请求，ID：{}，新数据：{}", id, toolUpdateDTO);
        iOverhaulToolService.updateInfoById(id,toolUpdateDTO);
        return JsonResult.ok();
    }
}
